package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //1.根据护理计划id获取出计划有关数据
        NursingPlan npp = nursingPlanMapper.selectById(id);
        //2.在根据护理计划id获取出中间表的数据
        //2.1构建查询条件
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(NursingProjectPlan::getPlanId, id);
        //2.2获取中间表数据
        List<NursingProjectPlan> list = nursingProjectPlanMapper.selectList(queryWrapper);
        //3.封装数据返回
        //3.1先构建NursingProjectPlanVo
        List<NursingProjectPlanVo> vos = new ArrayList<>();
        //3.2遍历list集合,将数据封装到NursingProjectPlanVo对象中
        list.forEach(nursingProjectPlan -> {
            NursingProjectPlanVo vo = NursingProjectPlanVo.builder()
                    .planId(nursingProjectPlan.getPlanId())
                    .projectId(String.valueOf(nursingProjectPlan.getProjectId()))
                    .executeTime(nursingProjectPlan.getExecuteTime())
                    .executeCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()))
                    .executeFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()))
                    .build();
            vos.add(vo);
        });
        //4.构建返回值
        NursingPlanVo nursingPlanVo = NursingPlanVo.builder()
                .id(npp.getId())
                .sortNo(npp.getSortNo())
                .planName(npp.getPlanName())
                .status(npp.getStatus())
                .projectPlans(vos)
                .build();
        return nursingPlanVo;
    }
/*    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }*/

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        //1.数据拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        //2.将护理计划内容保存到数据库表中
        save(nursingPlan);

        //3.获取计划中的护理项目数据
        List<NursingProjectPlan> list = dto.getProjectPlans();
        if (list != null && !list.isEmpty()) {
            //3.1遍历集合,将planId设置进去
            list.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
            //4.获取到护理计划的主键ID,将数据插到中间表中(调用mapper插入)
            iNursingProjectPlanService.saveBatch(list);
        }


        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //1.属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        //2.将数据修改到数据库表中去
        updateById(nursingPlan);
        //3.获取中间表的数据
        List<NursingProjectPlan> nursingProjectPlans = dto.getProjectPlans();
        if (nursingProjectPlans != null && !nursingProjectPlans.isEmpty()) {
            //4.说明中间表有数据,将其删除,再插入新数据
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper = Wrappers.lambdaQuery();
            //4.1构建条件(根据计划id删除中间表数据)
            queryWrapper.eq(NursingProjectPlan::getPlanId, dto.getId());
            nursingProjectPlanMapper.delete(queryWrapper);
            //5.把计划id设置到集合中去(前端对于新增的中间表数据,没有传计划id)
            nursingProjectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(dto.getId());
            });
        }

        //6.将新的数据插入中间表中
        iNursingProjectPlanService.saveBatch(nursingProjectPlans);
        return 1;

    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        //1.删除护理计划表中的数据
        removeByIds(Arrays.asList(ids));
        //2.删除中间表中的数据
        //2.根据ids查询中间表的数据
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(NursingProjectPlan::getPlanId, ids);
        nursingProjectPlanMapper.delete(queryWrapper);
        return 1;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
